﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class UIController : MonoBehaviour
{
	#region 单例模型
	private static UIController _instance;
	private void Awake() { _instance = this; }
	public static UIController Instance => _instance;
	#endregion


	public SkillUI[] skills;
	public GameObject btnStart;
	public GameObject btnRestart;
	public GameObject btnMenu;
	public GameObject btnExit;
	public GameObject btnContinue;
	public GameObject pnlMenu;
	public Text txtPrompt;
	public Toggle tglGod;
	public Transform pnlStar;
	public Slider HP, MP;


	private void Start() {
		OnToggle(tglGod.isOn = GameController.Instance.IsGod);
	}

	#region 设置PlayerPanel

	/// <summary>
	/// 设置初始HP, MP
	/// </summary>
	/// <param name="player"></param>
	public void SetInitHPAndMP(Player player) {
		HP.minValue = 0; HP.maxValue = player.Hp;
		MP.minValue = 0; MP.maxValue = player.Mp;
		HP.value = player.CurrentHp;
		MP.value = player.CurrentMp;
	}

	/// <summary>
	/// 设置当前HP, MP
	/// </summary>
	/// <param name="player"></param>
	public void SetHPAndMP(Player player) {
		HP.value = player.CurrentHp;
		MP.value = player.CurrentMp;
	}
	#endregion



	#region 设置SkillPanel

	/// <summary>
	/// 使用技能, 进入冷却状态
	/// </summary>
	/// <param name="skillType"></param>
	public void UseSkill(SkillUIType skillType) {
		foreach (SkillUI skill in skills) {
			if (skill.type == skillType) {
				skill.CloseSkill();
			}
		}
	}

	/// <summary>
	/// 释放技能, 恢复正常状态
	/// </summary>
	/// <param name="skillType"></param>
	public void ReleaseSkill(SkillUIType skillType) {
		foreach (SkillUI skill in skills) {
			if (skill.type == skillType) {
				skill.OpenSkill();
			}
		}
	}


	#endregion


	/// <summary>
	/// 游戏结束, 弹出游戏结算结果
	/// </summary>
	public void EndGameUI(int star) {
		pnlMenu.SetActive(true);
		if (star == 0)
			txtPrompt.text = "游戏结束";
		else
			txtPrompt.text = "恭喜通关";
		btnContinue.SetActive(false);
		btnMenu.SetActive(false);
		SetStarNum(star);
	}


	/// <summary>
	/// 设置星星数量
	/// </summary>
	/// <param name="num"></param>
	private void SetStarNum(int num) {
		for (int i = 0; i < num; i++) {
			pnlStar.GetChild(i).GetComponent<Image>().color = Color.white;
		}
	}


	#region Button点击事件

	/// <summary>
	/// 打开游戏菜单, 同时暂停游戏
	/// </summary>
	public void OnOpenGameMenuClick() {
		GameController.Instance.PauseGame();
		txtPrompt.text = "暂停中";
		pnlMenu.SetActive(true);
		btnMenu.SetActive(false);
	}


	/// <summary>
	/// 退出游戏
	/// </summary>
	public void OnExitGameClick() {
		GameController.Instance.ExitGame();
	}

	/// <summary>
	/// 继续游戏
	/// </summary>
	public void OnContinueGameClick() {
		GameController.Instance.ContinueGame();
		pnlMenu.SetActive(false);
		btnMenu.SetActive(true);
	}

	/// <summary>
	/// 开始游戏
	/// </summary>
	public void OnStartGameClick() {
		CloseStartButton();
		GameController.Instance.BeginGame(false);   // 第一次进入游戏默认非上帝模式
	}

	/// <summary>
	/// 重新开始游戏
	/// </summary>
	public void OnReStartGameClick() {
		GameController.IsNewGame = true;
		GameController._isGod = tglGod.isOn;
		SceneManager.LoadScene(0);
	}

	public void OnToggle(bool b) {
		Color c = tglGod.GetComponent<Image>().color;
		if (b) {
			tglGod.GetComponent<Image>().color = new Color(c.r, c.g, c.b, 1);
		} else {
			tglGod.GetComponent<Image>().color = new Color(c.r, c.g, c.b, 0.4f);
		}
	}

	#endregion

	/// <summary>
	/// 关闭开始游戏的Button
	/// </summary>
	public void CloseStartButton() {
		btnStart.SetActive(false);
	}
}

